﻿
namespace Anycmd.Host
{
    using Anycmd.AC.Infra;
    using Exceptions;
    using Model;
    using System;

    /// <summary>
    /// 表示系统菜单业务实体。
    /// </summary>
    public sealed class MenuState : StateObject<MenuState>, IMenu
    {
        private IACDomain _acDomain { get; set; }
        private Guid _appSystemID;
        private Guid? _parentID;
        private string _name;
        private string _url;
        private string _icon;
        private int _sortCode;
        private int? _allowEdit;
        private int? _allowDelete;
        private string _description;

        private MenuState(Guid id) : base(id) { }

        public static MenuState Create(IACDomain acDomain, IMenu menu)
        {
            if (menu == null)
            {
                throw new ArgumentNullException("menu");
            }
            if (!acDomain.AppSystemSet.ContainsAppSystem(menu.AppSystemID))
            {
                throw new ValidationException("意外的应用系统标识" + menu.AppSystemID);
            }
            return new MenuState(menu.Id)
            {
                _acDomain = acDomain,
                _appSystemID = menu.AppSystemID,
                _name = menu.Name,
                _parentID = menu.ParentID,
                _url = menu.Url,
                _icon = menu.Icon,
                _sortCode = menu.SortCode,
                _allowDelete = menu.AllowDelete,
                _allowEdit = menu.AllowEdit,
                _description = menu.Description
            };
        }

        public Guid AppSystemID
        {
            get { return _appSystemID; }
        }

        public Guid? ParentID
        {
            get { return _parentID; }
        }

        public string Name
        {
            get { return _name; }
        }

        public string Url
        {
            get { return _url; }
        }

        public string Icon
        {
            get { return _icon; }
        }

        public int SortCode
        {
            get { return _sortCode; }
        }

        public int? AllowEdit
        {
            get { return _allowEdit; }
        }

        public int? AllowDelete
        {
            get { return _allowDelete; }
        }

        public string Description
        {
            get { return _description; }
        }

        protected override bool DoEquals(MenuState other)
        {
            return Id == other.Id &&
                AppSystemID == other.AppSystemID &&
                ParentID == other.ParentID &&
                Name == other.Name &&
                Url == other.Url &&
                Icon == other.Icon &&
                SortCode == other.SortCode &&
                AllowDelete == other.AllowDelete &&
                AllowEdit == other.AllowEdit &&
                Description == other.Description;
        }
    }
}
